import json
from pprint import pprint

from django.test import TestCase, Client

from jqdelivery.models import UserExpressInfo
from jqdelivery.serializers import UserExpressInfoSerializer
from jqpackage.tests import gen_user


class JqDeliveryTest(TestCase):
    def setUp(self):
        self.user = gen_user()
        self.wx_c = Client(HTTP_X_WX_OPENID=self.user.openid)

    def test_get_express(self):
        res = self.wx_c.post(
            "/api/delivery/express",
            {
                "express": {
                    "express_name": "蔡徐坤",
                    "express_phone": "13888888888",
                    "express_area": "北京市 北京市 朝阳区",
                    "express_address": "一个不存在的地方",
                },
                "default": True,
            },
            content_type="application/json",
        )
        data = json.loads(res.content)
        pprint(data)
        user_express_info = UserExpressInfo.objects.filter(deleted=False).first()
        user_express_info_data = UserExpressInfoSerializer(user_express_info).data

        res = self.wx_c.get(
            "/api/delivery/express", {"uuid": user_express_info_data["uuid"]}
        )
        data = json.loads(res.content)
        pprint(data)

        res = self.wx_c.put(
            "/api/delivery/express",
            {
                "express": {
                    "uuid": user_express_info_data["uuid"],
                    "express_name": "蔡不徐坤",
                    "express_phone": "138887777777",
                    "express_area": "北京市 北京市 北京区",
                    "express_address": "一个好像存在的地方",
                },
                "default": False,
            },
            content_type="application/json",
        )
        data = json.loads(res.content)
        pprint(data)
        pprint("删除用户地址")
        res = self.wx_c.delete(
            "/api/delivery/express",
            {"uuid": user_express_info_data["uuid"]},
            content_type="application/json",
        )
        data = json.loads(res.content)
        pprint(data)

        res = self.wx_c.get("/api/delivery/express/list")
        data = json.loads(res.content)
        pprint(data)
